package com.nondo.rdp.auth.endpoint;

import com.nondo.rdp.auth.domain.Menu;
import com.nondo.rdp.auth.interceptor.OperationLog;
import com.nondo.rdp.auth.service.MenuService;
import com.nondo.rdp.core.model.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.seelyn.superp.core.endpoint
 *
 * @author linfeng
 * @since 2017/7/30
 */
@RestController
@RequestMapping("/api/sys/menu")
public class MenuEndpoint {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public View<List<Menu>> findAllMenus(HttpServletRequest request, HttpServletResponse response) {

        List<Menu> list = new ArrayList<>();
        List<Menu> sourcelist = menuService.getMenuList(request, response);
        Menu.sortList(list, sourcelist, 1L);
        View<List<Menu>> menusView = View.ofOk();
        menusView.setData(list);
        return menusView;
    }

    /**
     * 菜单保存
     * @param menu
     * @return
     */
    @OperationLog
    @RequestMapping(value = "/save")
    public View<Menu> save(@Validated @RequestBody Menu menu) {

        menuService.save(menu);
        View<Menu> menuView = View.ofOk();
        menuView.setData(menu);
        return menuView;
    }

    /**
     * 菜单删除
     * @param id
     * @return
     */
    @OperationLog
    @RequestMapping(value = "/delete/{id}")
    public View<String> delete(@PathVariable("id") Long id) {
        if (Menu.isRoot(id)) {
            return View.ofError("删除菜单失败, 不允许删除顶级菜单或编号为空");
        } else {
            menuService.delete(id);
            return View.ofOk("删除菜单成功");
        }
    }

    /**
     * 菜单排序
     * @param ids
     * @param sorts
     * @param redirectAttributes
     * @return
     */
    @OperationLog
    @RequestMapping(value = "/updateSort")
    public View<String> updateSort(Long[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
        int len = ids.length;
        Menu[] menus = new Menu[len];
        for (int i = 0; i < len; i++) {
            menus[i] = menuService.findOne(ids[i]);
            menus[i].setSort(sorts[i]);
            menuService.save(menus[i]);
        }
        return View.ofOk("保存菜单排序成功");
    }

    /**
     * 菜单列表树
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "treeData")
    public View<List<Map<String, Object>>> treeData(HttpServletRequest request, HttpServletResponse response) {

        View<List<Map<String, Object>>> view = View.ofOk();
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Menu> list = menuService.findByRoot();
        initTree(list, mapList);
        view.setData(mapList);
        return view;
    }

    private void initTree(List<Menu> list, List<Map<String, Object>> mapList) {

        for (Menu menu : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", menu.getId());
            map.put("label", menu.getName());
            map.put("icon", menu.getIcon());
            map.put("sort", menu.getSort());
            map.put("href", menu.getHref());
            map.put("permission", menu.getPermission());
            map.put("show", menu.getShow());

            List<Menu> result = menu.getChildList();
            if (!result.isEmpty()) {
                List<Map<String, Object>> childList = new ArrayList<>();
                initTree(result, childList);
                map.put("children", childList);
            }

            mapList.add(map);
        }

    }

}
